import { createId } from "@paralleldrive/cuid2";
import type { InferInsertModel, InferSelectModel } from "drizzle-orm";
import { json, pgTable, text, varchar } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import type { Extra, Issue, Label, Parameter, Stage, Status, Time } from "../types/allure";

export const tests = pgTable("tests", {
  id: varchar({ length: 255 })
    .primaryKey()
    .$defaultFn(() => createId()),
  name: varchar({ length: 255 }).notNull(),
  time: json().$type<Time>().notNull(),
  status: varchar({ length: 255 }).$type<Status>().notNull(),
  statusMessage: text("status_message"),
  statusTrace: text("status_trace"),
  beforeStages: json("before_stages").$type<Stage[]>().default([]),
  testStage: json("test_stages").$type<Stage>(),
  afterStages: json("after_stages").$type<Stage[]>().default([]),
  labels: json().$type<Label[]>().default([]),
  parameters: json().$type<Parameter[]>().default([]),
  extra: json().$type<Extra>().notNull(),
  parameterValues: json().$type<string[]>().default([]),
  // extra
  issue: varchar({ length: 255 }).$type<Issue>(),
  editer: varchar({ length: 255 }),
  reportId: varchar({ length: 255 }).notNull(),
});

export type Test = InferSelectModel<typeof tests>;
export type TestInsert = InferInsertModel<typeof tests>;
export const testsInsertSchema = createInsertSchema(tests);
export const testsSelectSchema = createSelectSchema(tests);
